package org.ocean.online.wicket;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.wicket.markup.MarkupType;
import org.apache.wicket.protocol.http.servlet.MultipartServletWebRequest;
import org.apache.wicket.util.upload.FileItem;
import org.ocean.online.constant.Constant;
import org.ocean.online.utils.UserUtil;

public class UploadifyFileProcessPage extends BaseWebPage{

	private static final long serialVersionUID = 7958651102956326627L;

	public UploadifyFileProcessPage() {
		MultipartServletWebRequest r = (MultipartServletWebRequest) this.getRequest();
		String path = this.getStringParam("path");
		String username = this.getStringParam(Constant.Username_VariableName);
		Collection<List<FileItem>> files = r.getFiles().values();
		for(List<FileItem> list:files){
			for(FileItem fi : list){
//				String fullPath = UserUtil.getUserDataPath(username)+username+File.separator+path+File.separator+fi.getName();
				try {
					UserUtil.saveFile(username, path+File.separator+fi.getName(), fi.getInputStream());
//					FileUtils.writeByteArrayToFile(new File(fullPath),fi.get());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		}
	}
	@Override
	public MarkupType getMarkupType() {
		return new MarkupType("txt", "plain/text");
	}

	@Override
	public void renderPage() {
		this.getResponse().write("upload success");
	}
}
